﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fcwms.Common;

namespace fcwms.DAL
{
    /// <summary>
    /// 数据访问类:dt_chuku
    /// </summary>
    public partial class dt_chuku
    {
        public dt_chuku()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dt_chuku");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在报关单号
        /// </summary>
        public bool Exists(string decla_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dt_chuku");
            strSql.Append(" where decla_id=@decla_id");
            SqlParameter[] parameters = new SqlParameter[]{
                    new SqlParameter("@decla_id",decla_id)};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 返回出库仓库
        /// </summary>
        //public string GetChukuStorage(int id)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select top 1 chuku_storage from dt_chuku");
        //    strSql.Append(" where id=@id ");
        //    SqlParameter[] parameters = new SqlParameter[] { 
        //            new SqlParameter("@id",id)};
        //    string chuku_storage = DbHelperSQL.GetSingle(strSql.ToString(), parameters).ToString();
        //    if (string.IsNullOrEmpty(chuku_storage))
        //    {
        //        return "";
        //    }
        //    return chuku_storage;
        //}

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(fcwms.Model.dt_chuku model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_chuku(");
            strSql.Append("decla_id,chuku_way,chuku_time,customs_broker,unstuffing_group,tallyman,customer,draw_man,transactor,days,cost,remark)");
            strSql.Append(" values (");
            strSql.Append("@decla_id,@chuku_way,@chuku_time,@customs_broker,@unstuffing_group,@tallyman,@customer,@draw_man,@transactor,@days,@cost,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@chuku_way", SqlDbType.NVarChar,50),
					new SqlParameter("@chuku_time", SqlDbType.DateTime),
					new SqlParameter("@customs_broker", SqlDbType.NVarChar,100),
					new SqlParameter("@unstuffing_group", SqlDbType.NVarChar,100),
					new SqlParameter("@tallyman", SqlDbType.NVarChar,100),
					new SqlParameter("@customer", SqlDbType.NVarChar,100),
					new SqlParameter("@draw_man", SqlDbType.NVarChar,100),
					new SqlParameter("@transactor", SqlDbType.NVarChar,100),
					new SqlParameter("@days", SqlDbType.Int,4),
					new SqlParameter("@cost", SqlDbType.Decimal,9),
					new SqlParameter("@remark", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.decla_id;
            parameters[1].Value = model.chuku_way;
            parameters[2].Value = model.chuku_time;
            parameters[3].Value = model.customs_broker;
            parameters[4].Value = model.unstuffing_group;
            parameters[5].Value = model.tallyman;
            parameters[6].Value = model.customer;
            parameters[7].Value = model.draw_man;
            parameters[8].Value = model.transactor;
            parameters[9].Value = model.days;
            parameters[10].Value = model.cost;
            parameters[11].Value = model.remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(fcwms.Model.dt_chuku model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_chuku set ");
            strSql.Append("decla_id=@decla_id,");
            strSql.Append("chuku_way=@chuku_way,");
            strSql.Append("chuku_time=@chuku_time,");
            strSql.Append("customs_broker=@customs_broker,");
            strSql.Append("unstuffing_group=@unstuffing_group,");
            strSql.Append("tallyman=@tallyman,");
            strSql.Append("customer=@customer,");
            strSql.Append("draw_man=@draw_man,");
            strSql.Append("transactor=@transactor,");
            strSql.Append("days=@days,");
            strSql.Append("cost=@cost,");
            strSql.Append("remark=@remark");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@chuku_way", SqlDbType.NVarChar,50),
					new SqlParameter("@chuku_time", SqlDbType.DateTime),
					new SqlParameter("@customs_broker", SqlDbType.NVarChar,100),
					new SqlParameter("@unstuffing_group", SqlDbType.NVarChar,100),
					new SqlParameter("@tallyman", SqlDbType.NVarChar,100),
					new SqlParameter("@customer", SqlDbType.NVarChar,100),
					new SqlParameter("@draw_man", SqlDbType.NVarChar,100),
					new SqlParameter("@transactor", SqlDbType.NVarChar,100),
					new SqlParameter("@days", SqlDbType.Int,4),
					new SqlParameter("@cost", SqlDbType.Decimal,9),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.decla_id;
            parameters[1].Value = model.chuku_way;
            parameters[2].Value = model.chuku_time;
            parameters[3].Value = model.customs_broker;
            parameters[4].Value = model.unstuffing_group;
            parameters[5].Value = model.tallyman;
            parameters[6].Value = model.customer;
            parameters[7].Value = model.draw_man;
            parameters[8].Value = model.transactor;
            parameters[9].Value = model.days;
            parameters[10].Value = model.cost;
            parameters[11].Value = model.remark;
            parameters[12].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_chuku ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_chuku ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_chuku GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,decla_id,chuku_way,chuku_time,customs_broker,unstuffing_group,tallyman,customer,draw_man,transactor,days,cost,remark from dt_chuku ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            fcwms.Model.dt_chuku model = new fcwms.Model.dt_chuku();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_chuku DataRowToModel(DataRow row)
        {
            fcwms.Model.dt_chuku model = new fcwms.Model.dt_chuku();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["decla_id"] != null)
                {
                    model.decla_id = row["decla_id"].ToString();
                }
                if (row["chuku_way"] != null)
                {
                    model.chuku_way = row["chuku_way"].ToString();
                }
                if (row["chuku_time"] != null && row["chuku_time"].ToString() != "")
                {
                    model.chuku_time = DateTime.Parse(row["chuku_time"].ToString());
                }
                if (row["customs_broker"] != null)
                {
                    model.customs_broker = row["customs_broker"].ToString();
                }
                if (row["unstuffing_group"] != null)
                {
                    model.unstuffing_group = row["unstuffing_group"].ToString();
                }
                if (row["tallyman"] != null)
                {
                    model.tallyman = row["tallyman"].ToString();
                }
                if (row["customer"] != null)
                {
                    model.customer = row["customer"].ToString();
                }
                if (row["draw_man"] != null)
                {
                    model.draw_man = row["draw_man"].ToString();
                }
                if (row["transactor"] != null)
                {
                    model.transactor = row["transactor"].ToString();
                }
                if (row["days"] != null && row["days"].ToString() != "")
                {
                    model.days = int.Parse(row["days"].ToString());
                }
                if (row["cost"] != null && row["cost"].ToString() != "")
                {
                    model.cost = decimal.Parse(row["cost"].ToString());
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,decla_id,chuku_way,chuku_time,customs_broker,unstuffing_group,tallyman,customer,draw_man,transactor,days,cost,remark ");
            strSql.Append(" FROM dt_chuku ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,decla_id,chuku_way,chuku_time,customs_broker,unstuffing_group,tallyman,customer,draw_man,transactor,days,cost,remark ");
            strSql.Append(" FROM dt_chuku ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dt_chuku ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
